home *** CD-ROM | disk | FTP | other *** search
- Path: news.compuserve.com!newsmaster
- From: Philippe Verdy <100105.3120@compuserve.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Dynamically initializing static members
- Date: 24 Mar 1996 12:23:34 GMT
- Organization: CompuServe Incorporated
- Message-ID: <4j3es6$pd6@arl-news-svc-2.compuserve.com>
- NNTP-Posting-Host: dd22-055.compuserve.com
-
- martijnl@xs4all.nl (Martijn Lievaart) s'Θcrit :
- > class MYCLASS {
- > static unsigned int Registration_ID
- >
- > ...
- >
- > }
- > unsigned int MYCLASS::Registration_D = Registrate("MYCLASS");
- >
- > should do the trick (you where almost there!).
- >
- > apardon@rc1.vub.ac.be (Antoon Pardon) wrote:
- >
- > >I am playing with the idea of somekind of classregistration.
- >
- > >The registration class would provide for a function:
- >
- > >unsigned int Registrate(const char *name);
- >
- > >This would keep the name of the function stored somewhere and
- > >would return a registration ID.
- >
- > >The idea would be to include a static int into the class that
- > >would store this ID something like this
- >
- > >class MYCLASS {
- > > static unsigned int
- > > Registration_ID
- >
- > > ...
- >
- > >}
- >
- > >The problem now is how do I assign this Registration_ID
- > >automatically. In Modula 2 you have initialisation code
- > >that is called at the start of the program where you might
- > >then put code similar to
- >
- > >MYCLASS::Registration_D = Registrate("MYCLASS");
- >
- > >Is something similar possible in C++ or do I have to
- > >do it in a different way? I looked at the FAQ but
- > >couldn't find an answer there.
- >
- >
- > /~~~~~| /~~~~~| /~~~~~~|~~~~~\~~~~~\~~~~|~~~~~| We now return to our
- > / |/ |/ | o | o | | +-| regularly scheduled
- > / /| /| | /| | ___/ ___/ | +-| flame-throwing
- > ../___/.|____/.|___|__/~|___|_|..|__|..|_____|_____|...martijnl@xs4all.nl..
- >
- Your solution will work provided you implement a Registrator
- class to do that work.
- This Registrator class will have no instance but will only
- use static members, such as a registered class list, an ID
- generator, and the Registrate method.
- The generated ID can be an index within the vector<Registration>
- static member. The Registration simply stores the class name
- and may be other type informations.
- If you want to implement persistent objects, you should care of
- the order in which registration of classes occur.
- So your persistent stream should always store not only the ID
- (for efficiency) but also the map from name to ID, so that
- matching is possible when reloading later your persistent
- objects in future versions of your application, or in other
- programs.
- To facilitate this conversion, you can differentiate the global
- and running class ID from the streamed class ID, by providing
- a local map for these types (when loading your objects, find
- their type by name, then get its associated in-stream ID, and
- create a memory map between this read ID and the global ID,
- so that you can identify types coming from the input stream.
- Good luck !
-